import gui.*;
import backend.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.DefaultEditorKit;
import org.apache.commons.configuration.*;
import org.tinyradius.util.*;
import java.util.*;

public class FReeconf extends JFrame {
    protected Server s;
    protected JFrame login_frame;

    // Check if we are on Mac OS X.  This is crucial to loading and using the OSXAdapter class.
    public static boolean MAC_OS_X = System.getProperty("os.name").startsWith("Mac OS X");

    // Define sizes of elements for uniformity
    final static Dimension LOGIN_SIZE = new Dimension(200,100);

    // Login Window
    public FReeconf() {
        super();
        this.login_frame = this;

        Dimension screen_size = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        Point loc = new Point((screen_size.width - LOGIN_SIZE.width)/2,
                              (screen_size.height - LOGIN_SIZE.height)/4);
        this.setTitle("Server Chooser");
        this.setMinimumSize(new Dimension(200,200));
        this.setLocation(loc);

        JPanel login_panel = new JPanel();
        login_panel.setLayout(new BoxLayout(login_panel, BoxLayout.Y_AXIS));
        login_panel.setPreferredSize(LOGIN_SIZE);

        final XMLConfiguration config = new XMLConfiguration();
        try {
            config.setFileName("src/config/servers.xml");
            config.load();
        }
        catch (ConfigurationException e) {
            JOptionPane.showMessageDialog(this, "The configuration was corrupted.  Verily, thou must fixeth it.",
                                          "Fatal Error", JOptionPane.ERROR_MESSAGE);
            System.exit(-1);
        }

        // Server choice subpanel
        JPanel server_subpanel = new JPanel();
        server_subpanel.add(new JLabel("Choose Server:"));

        java.util.List server_names = config.getList("server[@filename]");
        //XXX This breaks on one server because you can't cast a single object to a collection
        final JComboBox server_list = new JComboBox(new Vector(server_names));
        //TODO: Make this editable and actually work with all the edge cases
        //server_list.setEditable(true);
        server_subpanel.add(server_list);
        login_panel.add(server_subpanel);

        // Username input subpanel
        JPanel username_subpanel = new JPanel();
        username_subpanel.add(new JLabel("Username:"));

        final JTextField username_in = new JTextField(15);
        username_subpanel.add(username_in);
//         login_panel.add(username_subpanel);

        // Password input subpanel
        JPanel password_subpanel = new JPanel();
        password_subpanel.add(new JLabel("Password:"));

        final JPasswordField password_in = new JPasswordField(15);
        password_subpanel.add(password_in);
//         login_panel.add(password_subpanel);

        // It's a login button, yo.
        JPanel button_subpanel = new JPanel();
        JButton submit_button = new JButton("Configure");
        submit_button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String username = username_in.getText();
                    char[] password = password_in.getPassword();
                    String server_name = (String)server_list.getSelectedItem();

                    // XXX Here's how we talk to RADIUS to authenticate
                    // RadiusClient rc = new RadiusClient(host, sharedSecret);
                    // XXX Mind the password here, JPasswordField gives us some crap
                    // if (rc.authenticate(username, password)) {
                    // Set the configuration to the subnode of this server
                    try {
                        XMLConfiguration xml = new XMLConfiguration("src/config/"+server_name+".xml");
                        xml.setFileName(server_name+".xml");
                        Server s = new Server(xml, server_name);
                        login_frame.setVisible(false);
                        JFrame manager = new ManagementFrame(login_frame, s);
                    }
                    catch (ConfigurationException ce) {
                        JOptionPane.showMessageDialog(null, "Server wasn't found for some reason.",
                                                      "Error", JOptionPane.ERROR_MESSAGE);
                    }
                    // }
                    // else {
                    //     JOptionPane.showMessageDialog(null, "Username or Password invalid.",
                    //                                   "Error", JOptionPane.ERROR_MESSAGE);
                    // }
                }
            });
        button_subpanel.add(submit_button);

        JButton new_server_button = new JButton("New Server");
        new_server_button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // This is the easiest way is to just destroy the current
                    // login window and create it again when we're done with the
                    // new server configuration.  The "better" way is to have an
                    // event that tells the login_frame to update its model and
                    // just change visibilities.
                    JFrame new_server = new NewServerFrame();
                    login_frame.setVisible(false);
                    login_frame.dispose();
                }
            });
        button_subpanel.add(new_server_button);
        login_panel.add(button_subpanel);

        this.getContentPane().add(login_panel);
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new FReeconf();
                }
            });
    }
}

